//权限分类管理

interface IAuthorizationTypeManagentDetailViewModel {
    /**
     * 编号（编辑用）
     */
    id?: string;
    /**
     * 权限分类名称
     */
    name: string;
    /**
     * 权限分类级别
     */
    level: string;
    /**
     * 保存事件
     */
    click: () => void;
}

interface IAuthorizationTypeManagentViewModel extends IBaseViewModel<IAuthorizationTypeQueryTerms> {
    /**
     * 权限分类级别下拉
     */
    levelList: Array<ValueListItemResponse>;
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<AuthorizationTypeListItemResponse>;
    /**
     * 添加/编辑视图
     */
    detail: IAuthorizationTypeManagentDetailViewModel;
    /**
     * 添加权限分类
     */
    addAuthorizationType: () => void;
    /**
     * 编辑权限分类
     */
    editAuthorizationType: (id: string) => void;
    /**
     * 删除权限分类
     */
    delAuthorizationType: (id: string) => void;
    /**
     * 查询
     */
    query:() => void;
    /**
     * 列表高度
     */
    heightA:number
}

interface IAuthorizationTypeQueryTerms {
    /**
     * 名称
     */
    name: string;
}

class AuthorizationTypeManagentController extends ListController<IAuthorizationTypeManagentViewModel, IAuthorizationTypeQueryTerms> {
    gridApi: uiGrid.IGridApiOf<AuthorizationTypeListItemResponse>;

    constructor($scope: IAuthorizationTypeManagentViewModel, private authorizationTypeService: IAuthorizationTypeService, private valueService: IValueService,
        private $ngBootbox: BootboxService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
        this.$scope.addAuthorizationType = this.showAddAuthorizationTypeDialog;
        this.$scope.editAuthorizationType = this.showEditAuthorizationTypeDialog;
        this.$scope.delAuthorizationType = this.showDeleteAuthorizationTypeDialog;

        this.$scope.queryTerms = {
            name: ""
        };

        this.$scope.gridOptions = {
            enableSorting: false,
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            paginationPageSizes: [10, 20, 30],
            columnDefs: [
                { name: "序号", field: 'index', enableColumnMenu: false, sort: false },
                { name: "权限名称", field: 'name', enableColumnMenu: false },
                { name: '权限级别', field: 'level', enableColumnMenu: false },
                {
                    name: "操作", field: '操作',
                    cellFilter: 'checkmark',
                    cellTemplate: '<a data-toggle="modal" data-target="#myModal" title="编辑" class="fa fa-edit m-l-xs primary text-info" ng-click="grid.appScope.editAuthorizationType(row.entity.id)"></a> <a class="fa fa-trash-o primary text-danger" title="删除" ng-click="grid.appScope.delAuthorizationType(row.entity.id)"></a> ',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };
        this.$scope.query = this.query;
        this.query();
        this.loadLevelDropDown();
    }

    loadLevelDropDown(): void {
        this.$scope.levelList = this.valueService.getSystemLevelList().data;
    }

    loadData(queryTerms: IAuthorizationTypeQueryTerms, skip: number, count: number): void {
        this.authorizationTypeService.getList(queryTerms.name, skip, count).then(result => {
            this.$scope.gridOptions.totalItems = result.data.total;
            result.data.data.map((item, index) => item.index = skip++ +1);
            this.$scope.gridOptions.data = result.data.data;

            this.$scope.heightA=result.data.data.length*39+95;

            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        });
    }

    showAddAuthorizationTypeDialog = () => {
        this.$scope.title = "添加权限分类";
        this.$scope.detail = {
            name: "",
            level: "",
            click: this.addAuthorizationType
        };
    }

    addAuthorizationType = () => {
        if (!$("#authTypeForm").valid()) { return };
        this.authorizationTypeService.add(
            this.$scope.detail.name,
            this.$scope.detail.level
        ).then(result => {
            this.reload();
            $('#myModal').modal('hide');
        });
    }

    showEditAuthorizationTypeDialog = (id: string) => {
        this.$scope.title = "编辑权限分类";
        var validator = $('#authTypeForm').validate();
        validator.form();
        validator.hideErrors();
        $(".error").removeClass("error");
        this.authorizationTypeService.getDetail(id).then(result => {
            this.$scope.detail = {
                id: id,
                name: result.data.name,
                level: result.data.level.toString(),
                click: this.editAuthorizationType
            }
        });
    }

    editAuthorizationType = () => {
        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            this.authorizationTypeService.edit(
                this.$scope.detail.id,
                this.$scope.detail.name,
                this.$scope.detail.level
            ).then(result => {
                this.reload();
                $('#myModal').modal('hide');
            });
        });
    }

    showDeleteAuthorizationTypeDialog = (id: string) => {
        this.$ngBootbox.confirm("确定删除该数据吗？").then(e => {
            this.authorizationTypeService.delete(id).then(result => {
                this.reload();
                $('#myModal').modal('hide');
            });
        });
    }
}

angular.module("tms").controller('authorizationTypeManagentController', ['$scope', 'authorizationTypeService', 'valueService', '$ngBootbox', AuthorizationTypeManagentController]);